package com.framework.common.util.hump;

import com.framework.common.util.other.SymbolUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.common.util.hump
 * @description 驼峰和下划线互转工具类
 * @datetime 2019-01-09 11:11:11
 */
public class HumpOrLineUtil {
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * @param str 1 字符串
     * @return java.lang.String
     * @title 驼峰转下左斜线
     * @description 驼峰转下左斜线
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    public static String humpToSlash(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, SymbolUtil.NO_INPUT_METHOD_FORWARD_SLASH + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @param str 1 字符串
     * @return java.lang.String
     * @title 驼峰转下划线
     * @description 驼峰转下划线
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, SymbolUtil.NO_INPUT_METHOD_UNDERLINE + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @param str 1 字符串
     * @return java.lang.String
     * @title 下划线转驼峰
     * @description 下划线转驼峰
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @param str 1 字符串
     * @return java.lang.String
     * @title 获取字符串中的大写字母
     * @description 获取字符串中的大写字母
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    public static String getUppercaseLetter(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                sb.append(str.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * @param str 1 字符串
     * @return java.lang.String
     * @title 首字母大写
     * @description 首字母大写
     * @author 龘鵺
     * @datetime 2025-05-08 15:25:07
     */
    public static String stringToFirstOneUpperCase(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

}
